/*
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 *
 * sparse1.c
 *
 * Code generation for function 'sparse1'
 *
 */

/* Include files */
#include "sparse1.h"
#include "emxutil.h"
#include "types.h"
#include "rt_nonfinite.h"

/* Function Definitions */
void sparse_plus(const emxArray_real_T *a, const emxArray_real_T *b_d,
                 const emxArray_int32_T *b_colidx,
                 const emxArray_int32_T *b_rowidx, int b_m, int b_n,
                 emxArray_real_T *s)
{
  const double *a_data;
  const double *b_d_data;
  double *s_data;
  const int *b_colidx_data;
  const int *b_rowidx_data;
  int b_u0;
  int col;
  int fullColIncrement;
  int fullRowIncrement;
  int full_col;
  int idxInc;
  int row;
  int sparseColIncrement;
  int u0;
  boolean_T x[2];
  boolean_T exitg1;
  boolean_T y;
  b_rowidx_data = b_rowidx->data;
  b_colidx_data = b_colidx->data;
  b_d_data = b_d->data;
  a_data = a->data;
  u0 = a->size[0];
  if (u0 < b_m) {
    u0 = b_m;
  }
  b_u0 = a->size[1];
  if (b_u0 < b_n) {
    b_u0 = b_n;
  }
  idxInc = s->size[0] * s->size[1];
  s->size[0] = u0;
  s->size[1] = b_u0;
  emxEnsureCapacity_real_T(s, idxInc);
  s_data = s->data;
  sparseColIncrement = u0 * b_u0;
  for (idxInc = 0; idxInc < sparseColIncrement; idxInc++) {
    s_data[idxInc] = 0.0;
  }
  x[0] = (b_m == a->size[0]);
  x[1] = (b_n == a->size[1]);
  y = true;
  sparseColIncrement = 0;
  exitg1 = false;
  while ((!exitg1) && (sparseColIncrement < 2)) {
    if (!x[sparseColIncrement]) {
      y = false;
      exitg1 = true;
    } else {
      sparseColIncrement++;
    }
  }
  if (y) {
    sparseColIncrement = 1;
    idxInc = 1;
    fullRowIncrement = 1;
    fullColIncrement = 1;
  } else {
    if (b_m == 1) {
      sparseColIncrement = 1;
      idxInc = 0;
    } else if (b_n == 1) {
      sparseColIncrement = 0;
      idxInc = 1;
    } else {
      sparseColIncrement = 1;
      idxInc = 1;
    }
    if (a->size[0] == 1) {
      fullRowIncrement = 0;
      fullColIncrement = 1;
    } else if (a->size[1] == 1) {
      fullRowIncrement = 1;
      fullColIncrement = 0;
    } else {
      fullRowIncrement = 1;
      fullColIncrement = 1;
    }
  }
  full_col = 0;
  for (col = 0; col < b_u0; col++) {
    int idx;
    int idx_tmp;
    idx_tmp = col * sparseColIncrement;
    idx = b_colidx_data[idx_tmp];
    for (row = 0; row < u0; row++) {
      if ((idx < b_colidx_data[idx_tmp + 1]) &&
          ((row + 1 == b_rowidx_data[idx - 1]) || (idxInc == 0))) {
        s_data[row + s->size[0] * col] =
            a_data[row * fullRowIncrement + a->size[0] * full_col] +
            b_d_data[idx - 1];
        idx += idxInc;
      } else {
        s_data[row + s->size[0] * col] =
            a_data[row * fullRowIncrement + a->size[0] * full_col];
      }
    }
    full_col += fullColIncrement;
  }
}

/* End of code generation (sparse1.c) */
